<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="1600"
    draggable
    @close="close"
  >
    <el-dialog
      v-model="innerVisible"
      width="1000"
      title="任务提交详情xxx"
      top="3vh"
    >
      <div class="htmlBox" v-html="currShowSubmit.SubmitTaskMemo"></div>
      <template #footer>
        <div>
          <!-- :disabled="currShowSubmit.TaskState == 2 && StateAuth == 0" -->
          <el-select
            v-model="currShowSubmit.TaskState"
            style="width: 130px; margin-left: 10px"
            @change="onTaskState"
          >
            <el-option label="待审核" :value="1" />
            <el-option label="已审核通过" :value="2" />
            <el-option label="未通过" :value="-1" />
          </el-select>
          <el-button
            type="primary"
            style="margin-left: 10px"
            @click="editWorkTask(currShowSubmit)"
            @dblclick.prevent
          >
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <div>
      <el-form
        ref="formRef"
        label-width="150px"
        :model="form"
        :rules="isRules"
        style="display: flex; align-items: flex-end"
      >
        <div style="width: 80%">
          <!-- 选择产品活赠品 -->
          <el-form-item label="请选择产品或赠品" prop="ProductType">
            <el-select
              v-model="form.ProductType"
              :disabled="form.QuerenCount > 1"
              style="width: 20%"
              @change="ChangeProductType"
            >
              <el-option label="产品" :value="0" />
              <el-option label="赠品" :value="1" />
            </el-select>

            <el-select
              v-if="form.ProductType == 0"
              v-model="form.OrderListID"
              :disabled="form.QuerenCount > 1"
              style="width: calc(80% - 10px); margin-left: 10px"
              @change="reGetAreaStaff"
            >
              <el-option
                v-for="item in OrderList"
                :key="item.OrderListID"
                :label="item.ProductName"
                :value="item.OrderListID"
              />
            </el-select>
            <el-select
              v-if="form.ProductType == 1"
              v-model="form.OrderListID"
              :disabled="form.QuerenCount > 1"
              style="width: calc(80% - 10px); margin-left: 10px"
              @change="reGetAreaStaff"
            >
              <el-option
                v-for="item in GiftList"
                :key="item.OrderListID"
                :label="item.ProductName"
                :value="item.OrderListID"
              />
            </el-select>
          </el-form-item>
          <!-- 选择区域和员工 -->
          <el-form-item :label="title" prop="ProductType">
            <el-select
              v-model="form.AreaCode"
              :disabled="form.QuerenCount > 1"
              style="width: 20%"
              @change="selectAreaStaff"
            >
              <el-option
                v-for="(item, index) in StaffOptions"
                :key="index"
                :label="item.AreaName"
                :value="item.AreaCode"
              />
            </el-select>
            <el-select
              v-model="form.TaskStaffID"
              :disabled="form.QuerenCount > 1"
              filterable
              style="
                width: calc(80% - 210px);
                margin-left: 10px;
                margin-right: 10px;
              "
            >
              <el-option
                v-for="item in staffList"
                :key="item.StaffID"
                :label="item.StaffShow"
                :value="item.StaffID"
              />
            </el-select>
            <el-date-picker
              v-if="form.WorkTaskID"
              v-model="form.PlanFinishDate"
              :disabled="form.QuerenCount > 1"
              type="date"
              placeholder="任务整体计划完成日期"
              size="default"
              style="width: 190px"
              @change="setPlanFinishDate"
            />
          </el-form-item>
        </div>
        <div style="width: 20%; text-align: right; padding: 20px">
          <el-button
            type="primary"
            :disabled="form.QuerenCount > 1"
            @click="save"
            @dblclick.prevent
          >
            确 定
          </el-button>
          <br />
          <el-button
            style="margin-top: 15px"
            @click="dialogFormVisible = false"
          >
            取 消
          </el-button>
        </div>
      </el-form>
    </div>
    <div v-if="editType !== 0" style="width: 100%; height: auto">
      <div v-if="WorkTaskList.length">
        <el-table
          v-loading="listLoading"
          :data="WorkTaskList"
          @selection-change="setSelectRows"
        >
          <el-table-column
            v-if="form.QuerenCount > 1"
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="left"
            label="任务内容"
            prop="WorkTaskName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="单位"
            prop="WorkTaskUnit"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="指派数量"
            prop="WorkTaskNum"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            v-if="
              actstaffpostid == 5 ||
              actstaffpostid == 11 ||
              actstaffpostid == 42 ||
              actstaffpostid == 43
            "
            align="center"
            label="工单金额"
            prop="TichengMoney"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="计划完成日期"
            prop="PlanFinishDate"
            show-overflow-tooltip
            width="140"
          />
          <el-table-column align="center" label="备注" width="150">
            <template #default="{ row }">
              <div
                v-if="row.WorkTaskMemo"
                style="cursor: pointer"
                @click="editWorkTaskMemo(row)"
              >
                {{ row.WorkTaskMemo }}
              </div>
              <div
                v-else
                style="cursor: pointer"
                @click="editWorkTaskMemo(row)"
              >
                ---
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="实际完成数量"
            prop="FinishNum"
            show-overflow-tooltip
            width="120"
          />
          <el-table-column
            align="center"
            label="实际完成日期"
            prop="FinishDate"
            show-overflow-tooltip
            width="120"
          />
          <el-table-column align="center" label="查看提交详情" width="150">
            <template #default="{ row }">
              <el-button
                v-if="row.TaskState != 0"
                type="success"
                size="small"
                @click="showSubmit(row)"
              >
                质量查看审核
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="质量审核状态"
            prop="TaskState"
            width="140"
          >
            <template #default="{ row }">
              <div v-if="row.TaskState == 0">待提交完成</div>
              <div v-if="row.TaskState == 1">待审核</div>
              <div v-if="row.TaskState == 2">已审核通过</div>
              <div v-if="row.TaskState == -1">未通过</div>
              <div v-if="row.TaskState == -4">已作废</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="业绩审核状态"
            prop="TaskState"
            width="140"
          >
            <template #default="{ row }">
              <div v-if="row.SubmitCheckState === -1">有未审核</div>
              <div
                v-if="
                  row.SubmitCheckState === 0 ||
                  row.SubmitCheckState === 1 ||
                  row.SubmitCheckState == null
                "
              >
                未审核
              </div>
              <div v-if="row.SubmitCheckState === 2">审核通过</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="业绩查看审核"
            prop="TaskState"
            width="140"
          >
            <template #default="{ row }">
              <el-button
                v-if="row.SubmitList.length > 0"
                type="primary"
                size="small"
                @click="upDialogAudit(row)"
              >
                业绩查看审核
              </el-button>
              <div v-else>未提交业绩单</div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="form.QuerenCount <= 1"
            align="center"
            label="操作"
            fixed="right"
            width="150"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="editMsg(row)">
                修改
              </el-button>
              <el-button
                type="danger"
                size="small"
                @click="isWorkTaskListDel(row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div
          v-if="
            (selectRows.length && form.QuerenCount > 1) || form.AllCheckState
          "
          style="width: 100%; height: 60px; padding: 20px; background: #ffffcc"
        >
          <el-form
            ref="submitFormRef"
            :model="submitForm"
            style="display: flex; width: 600px"
          >
            <el-space>
              <el-form-item label="整体审核">
                <el-select
                  v-model="submitForm.AllCheckState"
                  style="width: 200px"
                >
                  <el-option label="请审核" :value="0" />
                  <el-option label="审核通过" :value="2" />
                  <el-option label="审核不通过" :value="-1" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="submitForm.AllCheckMemo"
                  placeholder="审核意见"
                  style="width: 300px"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="allAudit">提交审核</el-button>
              </el-form-item>
            </el-space>
          </el-form>
        </div>
      </div>
      <div v-else>
        <el-table v-loading="listLoading" :data="WorkTaskSetOption">
          <el-table-column
            align="left"
            label="任务内容"
            prop="WorkTaskName"
            show-overflow-tooltip
            width="230"
          />
          <el-table-column align="center" label="约定数量" width="150">
            <template #default="{ row }">
              {{ row.TaskNumSet }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="单位"
            prop="WorkTaskUnit"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column align="center" label="剩余可派数量" width="150">
            <template #default="{ row }">
              {{ row.isAssignCount }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="指派数量" width="150">
            <template #default="{ row, $index }">
              <el-input-number
                v-if="row.TaskNumSet"
                v-model="row.WorkTaskNum"
                :min="0"
                :max="row.isAssignCount"
                style="width: 120px"
                :disabled="form.QuerenCount > 1"
                @change="handleChangeNum($event, $index)"
              />
              <el-input-number
                v-else
                v-model="row.WorkTaskNum"
                :min="0"
                :max="10000"
                style="width: 120px"
                :disabled="form.QuerenCount > 1"
                @change="handleChangeNum($event, $index)"
              />
            </template>
          </el-table-column>
          <el-table-column
            v-if="
              actstaffpostid == 5 ||
              actstaffpostid == 11 ||
              actstaffpostid == 42 ||
              actstaffpostid == 43
            "
            align="center"
            label="工单单价"
            width="150"
          >
            <template #default="{ row }">
              {{ row.TichengPrice }}
            </template>
          </el-table-column>
          <el-table-column
            v-if="
              actstaffpostid == 5 ||
              actstaffpostid == 11 ||
              actstaffpostid == 42 ||
              actstaffpostid == 43
            "
            align="center"
            label="工单金额"
            width="150"
          >
            <template #default="{ row }">
              <el-input v-model="row.TichengMoney" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="计划完成日期" width="165">
            <template #default="{ row }">
              <!-- {{ row.WorkTaskNum }} -->
              <!-- <el-input v-model="row.PlanFinishDate" /> -->
              <el-date-picker
                v-model="row.PlanFinishDate"
                :disabled="form.QuerenCount > 1"
                type="date"
                placeholder="选择日期"
                size="default"
                style="width: 140px"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" label="备注" width="250">
            <template #default="{ row }">
              <el-input v-model="row.WorkTaskMemo" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- <div style="clear: both; margin-top: 20px; width: 100%; height: 100px">
        <el-form ref="taskformRef" :model="taskform" :rules="rules">
          <el-space>
            <el-form-item label="任务内容" prop="WorkTaskSetID">
              <el-select
                v-model="taskform.WorkTaskSetID"
                :disabled="form.QuerenCount > 1 || isModify"
                style="width: 200px"
                @change="changeWorkTask"
              >
                <el-option
                  v-for="(item, index) in WorkTaskSetOption"
                  :key="index"
                  :label="item.WorkTaskName"
                  :value="item.WorkTaskSetID"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="单位" prop="WorkTaskUnit">
              <el-select
                v-model="taskform.WorkTaskUnit"
                :disabled="form.QuerenCount > 1"
                style="width: 60px"
              >
                <el-option
                  :disabled="form.QuerenCount > 1"
                  label="个"
                  :value="1"
                />
              </el-select>
            </el-form-item>
            <el-form-item v-if="WorkTaskMsg.TaskNumSet == 0" label="约定数量">
              不限
            </el-form-item>
            <el-form-item v-else label="约定数量">
              {{ WorkTaskMsg.TaskNumSet }}
            </el-form-item>
            <el-form-item v-if="WorkTaskMsg.TaskNumSet == 0" label="可派数量">
              不限
            </el-form-item>
            <el-form-item v-else label="可派数量">
              {{ WorkTaskMsg.isAssignCount }}
            </el-form-item>
            <el-form-item label="指派数量" prop="WorkTaskNum">
              <el-input-number
                v-if="WorkTaskMsg.TaskNumSet == 0"
                v-model="taskform.WorkTaskNum"
                :min="0"
                style="width: 120px"
                :disabled="form.QuerenCount > 1"
                @change="handleChange"
              />
              <el-input-number
                v-else
                v-model="taskform.WorkTaskNum"
                :min="0"
                :max="WorkTaskMsg.isAssignCount"
                style="width: 120px"
                :disabled="form.QuerenCount > 1"
                @change="handleChange"
              />
              <span style="margin-left: 10px">
                {{ taskform.WorkTaskUnit }}
              </span>
            </el-form-item>
            <el-form-item label="计划完成日期" prop="PlanFinishDate">
              <el-date-picker
                v-model="taskform.PlanFinishDate"
                :disabled="form.QuerenCount > 1"
                type="date"
                placeholder="选择日期"
                size="default"
                style="width: 160px"
              />
            </el-form-item>
            <el-form-item label="工单金额" prop="TichengMoney">
              <el-input
                v-model="taskform.TichengMoney"
                type="number"
                placeholder="请输入工单金额"
                :disabled="form.QuerenCount > 1"
                style="width: 100px"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                v-if="!isModify"
                :icon="Search"
                type="primary"
                :disabled="form.QuerenCount > 1"
                plain
                @click="editWorkTask"
              >
                确认添加
              </el-button>
              <el-button
                v-else
                :icon="Search"
                type="primary"
                :disabled="form.QuerenCount > 1"
                plain
                @click="isWorkTaskListModify"
              >
                确认修改
              </el-button>
            </el-form-item>
          </el-space>
          <br />
          <el-space>
            <el-form-item label="任务备注" prop="WorkTaskMemo">
              <el-input
                v-model="taskform.WorkTaskMemo"
                :disabled="form.QuerenCount > 1"
                style="width: 630px"
              />
            </el-form-item>
          </el-space>
        </el-form>
      </div> -->
    </div>
    <el-dialog
      v-model="centerDialogVisible"
      :z-index="99999"
      append-to-body
      title="驳回"
      width="500"
      @close="centerDialogVisible = false"
    >
      <div style="margin: 0px 0px 10px 0px">请输入驳回原因</div>
      <div><el-input v-model="taskform.rejectReason" /></div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="centerDialogVisible = false">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </el-dialog>

  <el-dialog
    v-model="dialogAudit"
    title="业绩审核"
    width="1400px"
    @close="logClose"
  >
    <el-table v-loading="listLoading" :data="SubmitList">
      <el-table-column
        align="center"
        label="任务提交ID"
        prop="TaskSubmitID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="添加时间"
        prop="addTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="制作员工"
        prop="MakeStaffName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="工种"
        prop="MakeStaffPostName"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="
          actstaffpostid == 5 ||
          actstaffpostid == 11 ||
          actstaffpostid == 42 ||
          actstaffpostid == 43
        "
        align="center"
        label="提成单价"
        prop="TichengPrice"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="制作数量"
        prop="FinishNum"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="
          actstaffpostid == 5 ||
          actstaffpostid == 11 ||
          actstaffpostid == 42 ||
          actstaffpostid == 43
        "
        align="center"
        label="工单总金额"
        prop="TichengTotalMoney"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="
          actstaffpostid == 5 ||
          actstaffpostid == 11 ||
          actstaffpostid == 42 ||
          actstaffpostid == 43
        "
        align="center"
        label="提成比例"
        width="170"
      >
        <template #default="{ row, $index }">
          <el-input
            v-model="row.TichengRate"
            placeholder="请输入提成比例"
            style="float: left; width: 100px"
            @focus="rowedit($index)"
          />
          <el-button
            v-if="row.edit"
            type="primary"
            :icon="Edit"
            circle
            style="float: right"
            @click="calculate($index)"
          />
          <el-button
            v-else
            type="success"
            :icon="Check"
            circle
            style="float: right"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="
          actstaffpostid == 5 ||
          actstaffpostid == 11 ||
          actstaffpostid == 42 ||
          actstaffpostid == 43
        "
        align="center"
        label="提成金额"
        prop="TichengMoney"
        show-overflow-tooltip
      />
    </el-table>
    <div
      style="
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 10px;
      "
    >
      <el-form-item label="审核状态" prop="WorkTaskSetID" style="width: 100%">
        <el-select v-model="SubmitCheckState" style="width: 200px">
          <el-option
            v-for="(item, index) in auditOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <div style="display: flex; align-items: center; width: 50%">
          <div style="margin: 0px 10px">审核备注</div>
          <div style="width: 80%">
            <el-input v-model="SubmitCheckMemo" placeholder="请输入审核原因" />
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="SubmitCheck">确认</el-button>
      </el-form-item>
    </div>
    <!-- <template #footer>
      <el-button @click="logClose">取 消</el-button>
      <el-button type="primary" @click="receiveTask">确 定</el-button>
    </template> -->
  </el-dialog>
  <WorkTaskMemo ref="WorkTaskMemoRef" @reload-worklist="reloadWorkList" />
</template>

<script>
  import {
    AssignStaffSelect,
    WorkTaskListModify,
    WorkTaskListDel,
  } from '@/api/common'
  import {
    getWorkTaskSet,
    getWorkTaskList,
    saveWorkTask,
    CheckTaskSubmit,
    editSubmitList,
    allAuditSubmit,
  } from '@/api/worktask'
  import { saveOrderAssign } from '@/api/businessReviewsplitorders'
  import WorkTaskMemo from '@/views/components/WorkTaskMemo'
  import { ElMessage } from 'element-plus'
  import { Check, Edit, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'AssignStaff',
    components: { WorkTaskMemo },
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const { fetchData } = inject('rootMethods')

      const state = reactive({
        editMsg: {},
        isModify: false,
        WorkTaskMsg: {},
        WorkTaskMemoRef: null,
        SubmitCheckMemo: '',
        WorkTaskListID: 0,
        SubmitCheckState: 0,
        dialogAudit: false,
        SubmitList: [],
        GiftList: [],
        unitsTitle: '个',
        companyProps: {
          expandTrigger: 'hover',
        },
        deptmentProps: {
          expandTrigger: 'hover',
        },
        flag: false,
        rules: {
          WorkTaskSetID: [
            { required: true, trigger: 'blur', message: '请选择任务内容' },
          ],
          WorkTaskNum: [
            { required: true, trigger: 'blur', message: '请填写指派数量' },
          ],
          PlanFinishDate: [
            { required: true, trigger: 'blur', message: '计划完成日期' },
          ],
          TichengMoney: [
            { required: true, trigger: 'blur', message: '请填写工单金额' },
          ],
        },
        isRules: {
          OrderListID: [
            { required: true, trigger: 'blur', message: '请选择签约产品' },
          ],
          TaskStaffID: [
            { required: true, trigger: 'blur', message: '请先选择' },
          ],
        },
        organizationList: [],
        staffList: [],
        departmentList: [],
        centerDialogVisible: false,
        OrderID: 0,
        formRef: null,
        form: {
          ProductType: 0,
          isCopy: 0,
        },
        title: '',
        dialogFormVisible: false,
        OrderList: [],
        StoreList: [],
        StaffOptions: [],
        submitForm: {},
        submitFormRef: null,
        auditOptions: [
          {
            value: 0,
            label: '请选择审核状态',
          },
          // {
          //   value: 1,
          //   label: '暂不审核',
          // },
          {
            value: 2,
            label: '审核通过',
          },
          {
            value: -1,
            label: '未通过',
          },
        ],
        getStaffParams: [],
        taskformRef: null,
        taskform: {
          TichengMoney: 0,
          WorkTaskNum: 0,
        },
        WorkTaskSetOption: [],
        editType: 0,
        WorkTaskList: [],
        currShowSubmit: {},
        innerVisible: false,
        StateAuth: 0,
        isExceed: false,
        distributeAmount: 0,
        upWorkTask: {},
        editTichengRate: 0,
        editTichengMoney: 0,
        actstaffpostid: 0,
        selectRows: [],
        WorkTaskStateNum: 0,
      })
      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const setPlanFinishDate = () => {
        state.WorkTaskSetOption.forEach((item) => {
          item.PlanFinishDate = state.form.PlanFinishDate
        })
      }
      const showEdit = (
        OrderID,
        AssignSet,
        OrderList,
        WorkTask,
        GiftList,
        SaleStaffInfo,
        upWorkTask
      ) => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        state.userInfo = userInfo
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        // const actstaffpostid = actstaffpost[0][1]
        state.actstaffpostid = actstaffpost[0][1]

        state.upWorkTask = upWorkTask
        state.GiftList = GiftList
        state.form = Object.assign({}, WorkTask)
        state.submitForm.AllCheckState = state.form.AllCheckState
        state.submitForm.AllCheckMemo = state.form.AllCheckMemo
        if (!state.form.ProductType) {
          state.form.ProductType = OrderList[0].ProductType
        }

        console.log(state.form, '123')
        state.title = AssignSet.AssignOrderTargetName
        state.AssignSet = AssignSet
        state.OrderID = OrderID
        state.form.OrderID = OrderID
        // state.OrderList = []
        state.OrderList = OrderList
        if (!state.form.OrderListID) {
          state.form.OrderListID = state.OrderList[0].OrderListID
        }
        state.getStaffParams = AssignSet.AssignOrderTargetIDs
        state.editType = Object.keys(WorkTask).length
        console.log(WorkTask, 'WorkTask.lengthWorkTask.length')

        if (AssignSet.AssignOrderSetID === 1) {
          console.log('AssignSet.AssignOrderSetID', AssignSet.AssignOrderSetID)
          state.form.OrderID = OrderID
          state.form.AssignOrderSetID = AssignSet.AssignOrderSetID
        } else if (AssignSet.AssignOrderSetID == 3) {
          // 指派运营师
          const myOrderList = JSON.parse(JSON.stringify(OrderList[0]))
          console.log(myOrderList, 'myOrderList')
          state.form.OrderListID = myOrderList.OrderListID
          state.form.OrderID = myOrderList.OrderID
          state.form.AssignOrderSetID = AssignSet.AssignOrderSetID
          state.flag = true
          if (!state.form.OrderListID) {
            state.form.OrderListID = state.OrderList[0].OrderListID
          }
        } else if (AssignSet.AssignOrderSetID === 5) {
          state.form.OrderID = OrderID
          state.form.AssignOrderSetID = AssignSet.AssignOrderSetID
        } else if (AssignSet.AssignOrderSetID === 12 && !WorkTask.TaskCompany) {
          // 视频分单到销售人员 的情况下 自动展示本单的销售
          state.form = Object.assign({}, SaleStaffInfo)

          state.form.TaskStaffID = state.form.StaffID
          if (!state.form.ProductType) {
            state.form.ProductType = 0
          }
          if (!state.form.OrderListID) {
            state.form.OrderListID = state.OrderList[0].OrderListID
          }

          // console.log(state.form, '123')
          state.title = AssignSet.AssignOrderTargetName
          state.AssignSet = AssignSet
          state.OrderID = OrderID
          state.form.OrderID = OrderID
          // state.OrderList = []
          state.OrderList = OrderList
          state.getStaffParams = AssignSet.AssignOrderTargetIDs
          state.editType = Object.keys(WorkTask).length
          state.form.AssignOrderSetID = AssignSet.AssignOrderSetID
          console.log(state.form, '  state.form啥呀~！')
        } else {
          state.flag = false
          state.form.OrderID = OrderID
          state.form.AssignOrderSetID = AssignSet.AssignOrderSetID
          if (AssignSet.isCopy) state.form.isCopy = AssignSet.isCopy
        }

        if (state.editType === 0) {
          state.form.WorkTaskID = WorkTask.WorkTaskID
          state.taskform.WorkTaskID = WorkTask.WorkTaskID
        } else {
          state.editType = WorkTask.isList
          state.taskform.WorkTaskID = WorkTask.WorkTaskID
          state.taskform.WorkTaskListID = 0
          state.taskform.WorkTaskNum = 0
          getWorkTaskSets()
          getWorkTaskLists()
        }
        getStaffSelects()
        console.log(state.form, '22哈哈哈2')

        state.dialogFormVisible = true
      }
      const ChangeProductType = () => {
        state.form.OrderListID = null
        if (state.form.ProductType == 0) {
          state.form.OrderList = state.OrderList
          if (state.OrderList) {
            state.form.OrderListID = state.OrderList[0].OrderListID
          }
        } else {
          state.form.OrderList = state.GiftList
          if (state.GiftList) {
            state.form.OrderListID = state.GiftList[0].OrderListID
          }
        }
      }

      const reloadWorkList = () => {
        getWorkTaskLists()
      }

      const editWorkTaskMemo = (row) => {
        console.log('editWorkTaskMemo', row)
        state.WorkTaskMemoRef.showEdit(row)
      }
      const showSubmit = (row) => {
        state.currShowSubmit = row
        state.innerVisible = true
      }
      const SubmitCheck = async () => {
        console.log(state.SubmitList, 'state.审核')
        const dataMsg = {
          WorkTaskListID: state.WorkTaskListID,
          SubmitCheckState: state.SubmitCheckState,
          SubmitCheckMemo: state.SubmitCheckMemo,
          SubmitList: state.SubmitList,
        }
        const { msg } = await CheckTaskSubmit(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
        logClose()
        getWorkTaskLists()
        console.log(msg, '123123')
      }

      const upDialogAudit = (row) => {
        state.dialogAudit = true
        state.WorkTaskListID = row.WorkTaskListID
        state.SubmitList = row.SubmitList
        state.SubmitCheckState = row.SubmitCheckState
        console.log(row, '点击的那个')
      }

      const close = () => {
        emit('fetch-data')
        state.form = {
          ProductType: 0,
        }
        state.taskform = {
          WorkTaskListID: 0,
          TichengMoney: 0,
          WorkTaskNum: 0,
        }
        state.WorkTaskMsg = {}
        state.OrderList = []
        state.WorkTaskList = []
        state.staffList = []
        state.departmentList = []
        state.selectRows = []
        state.isModify = false
        state.dialogFormVisible = false
      }

      const isClose = () => {
        state.taskform.saveWorkTask = ''
        state.taskform = {
          WorkTaskID: state.taskform.WorkTaskID,
          WorkTaskListID: 0,
          TichengMoney: 0,
          WorkTaskNum: 0,
        }
        getWorkTaskLists()
      }

      const toReject = async () => {
        if (!state.taskform.rejectReason) {
          ElMessage({
            message: '驳回理由不能为空！',
            type: 'warning',
          })
        } else {
          const { msg } = await saveWorkTask(state.taskform)
          $baseMessage(msg, 'success', 'vab-hey-message-success')

          state.taskform = {
            rejectReason: '',
            WorkTaskID: state.taskform.WorkTaskID,
            WorkTaskListID: 0,
          }
          getWorkTaskLists()
          state.centerDialogVisible = false
        }
      }

      const editWorkTask = async (row) => {
        if (row.WorkTaskListID) {
          const rejectReason = state.taskform.rejectReason
          state.taskform = Object.assign({}, row)
          state.taskform.rejectReason = rejectReason
          state.taskform.TrackState = 1

          const { msg } = await saveWorkTask(state.taskform)

          $baseMessage(msg, 'success', 'vab-hey-message-success')
          state.innerVisible = false
          console.log('1111', row)

          console.log('2222', row)
          fetchData()
          getWorkTaskSets()
          getWorkTaskLists()
          state['taskformRef'].resetFields()
          state.taskform = {
            WorkTaskID: state.taskform.WorkTaskID,
            WorkTaskListID: 0,
            TichengMoney: 0,
            WorkTaskNum: 0,
            rejectReason: '',
          }
          state.WorkTaskMsg = {}
        } else {
          if (state.taskform.WorkTaskNum == 0) {
            ElMessage({
              message: '指派数量不能为0',
              type: 'warning',
            })
            return
          }
          state['taskformRef'].validate(async (valid) => {
            if (valid) {
              const { msg } = await saveWorkTask(state.taskform)

              $baseMessage(msg, 'success', 'vab-hey-message-success')
              state.innerVisible = false

              state['taskformRef'].resetFields()
              state.taskform = {
                WorkTaskID: state.taskform.WorkTaskID,
                WorkTaskListID: 0,
                TichengMoney: 0,
                WorkTaskNum: 0,
                rejectReason: '',
              }
              state.WorkTaskMsg = {}
              fetchData()
              getWorkTaskSets()
              getWorkTaskLists()
            }
          })
        }

        console.log('state.taskform', state.taskform)
      }

      const allAudit = () => {
        if (state.submitForm.AllCheckState) {
          state.submitForm.WorkTaskListIDs = state.selectRows
            .map((item) => item.WorkTaskListID)
            .join()
          state['formRef'].validate(async (valid) => {
            if (valid) {
              state.submitForm.WorkTaskID = state.form.WorkTaskID
              const { msg } = await allAuditSubmit(state.submitForm)
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              fetchData()
              close()
            }
          })
        } else {
          $baseMessage(
            '请务必进行审核并选择审核状态',
            'warning',
            'vab-hey-message-warning'
          )
        }
      }

      const save = () => {
        let WorkTaskNum = state.WorkTaskList.length

        state.WorkTaskSetOption.forEach((item) => {
          WorkTaskNum += item.WorkTaskNum
        })

        // console.log('指派任务总数量', WorkTaskNum, state.AssignSet.isList)

        if (state.form.WorkTaskID) {
          if (state.AssignSet.isList && WorkTaskNum === 0) {
            $baseMessage('请分配具体任务', 'error', 'vab-hey-message-error')
            return false
          }
        }

        // if (state.form.WorkTaskID) {
        //   if (state.AssignSet.isList && state.WorkTaskList.length === 0) {
        //     $baseMessage('请分配具体任务', 'error', 'vab-hey-message-error')
        //     return false
        //   }
        // }

        state['formRef'].validate(async (valid) => {
          if (valid) {
            state.form.upWorkTaskID = state.upWorkTask.WorkTaskID
            // console.log('state.upWorkTask', state.upWorkTask)
            if (state.WorkTaskList.length) {
              state.form.newWorkTask = []
            } else {
              state.form.newWorkTask = state.WorkTaskSetOption
            }

            const { msg } = await saveOrderAssign(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            fetchData()
            close()
          }
        })
      }

      const changeWorkTask = (row) => {
        console.log(row, '222')

        const res = state.WorkTaskSetOption.find((item) => {
          return item.WorkTaskSetID == row
        })
        console.log(res, 'resres')

        state.taskform.WorkTaskNum = 0
        state.WorkTaskMsg = res
        state.taskform.WorkTaskUnit = res.WorkTaskUnit
        state.taskform.TichengMoney =
          Number(res.TichengPrice) * Number(state.taskform.WorkTaskNum)
      }
      const handleChange = () => {
        if (state.WorkTaskMsg) {
          state.taskform.TichengMoney =
            Number(state.WorkTaskMsg.TichengPrice) *
            Number(state.taskform.WorkTaskNum)
        }
      }
      const handleChangeNum = (e, index) => {
        console.log('handleChangeNum', e, index)
        state.WorkTaskSetOption[index].TichengMoney =
          Number(state.WorkTaskSetOption[index].TichengPrice) *
          Number(state.WorkTaskSetOption[index].WorkTaskNum)
      }
      // 删除某条任务
      const isWorkTaskListDel = async (row) => {
        console.log(row, 'WorkTaskListIDWorkTaskListID')

        const dataMsg = {
          WorkTaskListID: row.WorkTaskListID,
        }
        const { msg } = await WorkTaskListDel(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
        fetchData()
        getWorkTaskLists()
      }

      const getWorkTaskLists = async () => {
        state.listLoading = true
        const getParams = {
          WorkTaskID: state.form.WorkTaskID,
        }
        const {
          data: { StateAuth, WorkTaskList },
        } = await getWorkTaskList(getParams)
        state.WorkTaskList = WorkTaskList
        state.WorkTaskList.forEach((item) => {
          state.WorkTaskStateNum += item.TaskState ? 1 : 0
        })
        state.StateAuth = StateAuth
        state.listLoading = false
      }

      const getStaffSelects = async () => {
        state.listLoading = true
        const getStaffParams = {
          AssignOrderSetID: state.AssignSet.AssignOrderSetID,
          OrderID: state.OrderID,
          StaffPostID: state.getStaffParams,
          OrderListID: state.form.OrderListID,
        }
        const {
          data: { StaffOptions },
        } = await AssignStaffSelect(getStaffParams)
        state.StaffOptions = StaffOptions
        if (StaffOptions.length !== 0) {
          if (!state.form.AreaCode) {
            state.form.AreaCode = state.StaffOptions[0].AreaCode
            selectAreaStaff(state.form.AreaCode)
          } else {
            selectAreaStaff(state.form.AreaCode)
          }
        }
        state.listLoading = false
      }
      // state.staffList = StaffOptions

      const getWorkTaskSets = async () => {
        state.listLoading = true
        const getParams = {
          AssignOrderSetID: state.form.AssignOrderSetID,
          WorkTaskID: state.form.WorkTaskID,
        }
        const {
          data: { WorkTaskSetOption },
        } = await getWorkTaskSet(getParams)
        state.WorkTaskSetOption = WorkTaskSetOption
        state.listLoading = false
      }

      const isWorkTaskListModify = async () => {
        if (state.taskform.WorkTaskNum == 0) {
          ElMessage({
            message: '指派数量不能为0',
            type: 'warning',
          })
          return
        }
        const { msg } = await WorkTaskListModify(state.taskform)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
        state.isModify = false
        state['taskformRef'].resetFields()
        state.taskform = {
          WorkTaskID: state.taskform.WorkTaskID,
          WorkTaskListID: 0,
          TichengMoney: 0,
          WorkTaskNum: 0,
        }
        state.WorkTaskMsg = {}
        fetchData()
        getWorkTaskSets()
        getWorkTaskLists()
      }
      const onTaskState = (e) => {
        console.log('选择：', e)
        if (e == -1) {
          state.centerDialogVisible = true
        }
      }
      const editMsg = (row) => {
        state.isModify = true
        state.taskform = Object.assign({}, row)
        getWorkTaskMsg(state.taskform.WorkTaskSetID)
      }
      const getWorkTaskMsg = (row) => {
        const res = state.WorkTaskSetOption.find((item) => {
          return item.WorkTaskSetID == row
        })
        console.log(res, '获取修改任务当前的数量信息')
        state.taskform.WorkTaskNum = 0
        state.WorkTaskMsg = Object.assign({}, res)
        // state.WorkTaskMsg.isAssignCount =
        //   state.WorkTaskMsg.isAssignCount + state.taskform.WorkTaskNum
        state.taskform.WorkTaskUnit = res.WorkTaskUnit
        state.taskform.TichengMoney =
          Number(res.TichengPrice) * Number(state.taskform.WorkTaskNum)
      }

      const logClose = () => {
        state.dialogAudit = false
      }
      const rowedit = (idx) => {
        state.SubmitList[idx].edit = true
        state.editTichengRate = state.SubmitList[idx].TichengRate
        state.editTichengMoney = state.SubmitList[idx].TichengMoney
      }

      onMounted(() => {})

      const selectAreaStaff = (e) => {
        console.log(state.StaffOptions, 'StaffOptionsStaffOptions')
        const res = state.StaffOptions.find((item) => {
          return item.AreaCode == e
        })
        state.staffList = res.StaffList
        console.log(state.staffList, '选择区域了')
      }

      // 计算比例
      const calculate = (idx) => {
        let molecule = 1
        let denominator = 1
        if (state.SubmitList[idx].TichengRate.split('/')[0]) {
          molecule = state.SubmitList[idx].TichengRate.split('/')[0]
        }
        if (state.SubmitList[idx].TichengRate.split('/')[1]) {
          denominator = state.SubmitList[idx].TichengRate.split('/')[1]
        }
        // 总金额大于0的时候才有计算 小于零怎么计算都是0
        if (state.SubmitList[idx].TichengTotalMoney > 0) {
          state.SubmitList[idx].TichengMoney =
            ((state.SubmitList[idx].TichengPrice * molecule) / denominator) *
            state.SubmitList[idx].FinishNum
          //保留两位小数点
          state.SubmitList[idx].TichengMoney =
            state.SubmitList[idx].TichengMoney.toFixed(2)
        } else {
          state.SubmitList[idx].TichengMoney = 0
        }

        if (
          Number(state.distributeAmount) +
            Number(state.SubmitList[idx].TichengMoney) >
          Number(state.SubmitList[idx].TichengTotalMoney)
        ) {
          state.SubmitList[idx].TichengRate = state.editTichengRate
          state.SubmitList[idx].TichengMoney = state.editTichengMoney
          ElMessage({
            message: '不可设置，添加后将超出总金额',
            type: 'warning',
          })
          state.isExceed = true
        } else {
          ElMessage({
            message: '已设置成功',
            type: 'success',
          })
          editSubmitList(state.SubmitList[idx])
          state.isExceed = false
        }

        state.SubmitList[idx].edit = false
      }

      return {
        ...toRefs(state),
        setSelectRows,
        showEdit,
        isWorkTaskListDel,
        selectAreaStaff,
        editMsg,
        getWorkTaskMsg,
        logClose,
        close,
        isClose,
        SubmitCheck,
        onTaskState,
        isWorkTaskListModify,
        save,
        changeWorkTask,
        getWorkTaskSets,
        editWorkTask,
        showSubmit,
        handleChange,
        toReject,
        upDialogAudit,
        rowedit,
        ChangeProductType,
        Check,
        Edit,
        Search,
        calculate,
        editWorkTaskMemo,
        reloadWorkList,
        handleChangeNum,
        setPlanFinishDate,
        allAudit,
      }
    },
  })
</script>
<style>
  .htmlBox img {
    width: 100%;
  }
</style>
